package com.bo.book.service;

import com.bo.book.enums.BookStatus;
import com.bo.book.mapper.BookMapper;
import com.bo.book.model.BookInfo;
import com.bo.book.dao.BookDao;
import com.bo.book.model.PageRequest;
import com.bo.book.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.List;
@Slf4j
@Service
// 业务逻辑层:从数据访问层获取信息,并对数据进行加工
public class BookService {
    // DI:依赖注入,相当于把Spring容器中的BookDao实例拿出来,注入到当前依赖中
    @Autowired
    private BookMapper bookMapper;

    public List<BookInfo> getList() {
        // 从IOC容器中拿,而不自己创建了
        //BookDao bookDao = new BookDao();
        List<BookInfo> bookInfos = bookMapper.selectBook();
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()));
        }
        return bookInfos;
    }

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    // 通过mapper层的方法来处理controller层的业务逻辑
    // 1.总图书个数(mapper层的count())
    // 2.当前图书页(mapper层的getListByPage())
    // 3.客户端请求
    // 注意:数据库中的图书不存在"statusCN",需在service层实现
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer count = bookMapper.count();
        List<BookInfo> list = bookMapper.getListByPage(pageRequest);
         for(BookInfo bookInfo : list) {
             // 此处与getList出现代码重复,同为循环逻辑,可用枚举类
             // if(bookInfo.getStatus() == 0){
             //     bookInfo.setStatusCN("无效");
             // }else if(bookInfo.getStatus() == 1){
             //     bookInfo.setStatusCN("正常");
             // }else{
             //     bookInfo.setStatusCN("不允许借阅");
             // }
             bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()));
         }
        PageResult pageResult = new PageResult<BookInfo>();
        pageResult.setTotal(count);
        pageResult.setList(list);
        pageResult.setPageRequest(pageRequest);
        return pageResult;
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public String updateBook(BookInfo bookInfo) {
        // 如果为null代表该值不修改,则不用做判断,反之则判断
        if(bookInfo.getCount() != null && bookInfo.getCount() < 0){
            return "修改的count为负数";
        }
        if(bookInfo.getPrice() != null &&
                bookInfo.getPrice().compareTo(new BigDecimal(0.0)) <= 0){
            return "修改的price为负数";
        }
        BookInfo book = bookMapper.queryBookById(bookInfo.getId());
        if(book == null){
            return "该id的图书不存在!";
        }
        bookMapper.updateBook(bookInfo);
        return "";
    }

    public String deleteBook(Integer bookId) {
        BookInfo book = bookMapper.queryBookById(bookId);
        if(book == null){
            return "该id的图书不存在!";
        }
        // 删除图书
        book.setStatus(0);
        bookMapper.updateBook(book);
        return "";
    }

    public String batchDeleteBook(List<Integer> ids) {
        for(Integer num : ids){
            if(num <= 0){
                return "批量删除图书失败";
            }
        }
        bookMapper.batchDeleteBook(ids);
        return "";
    }
}







